package cn.jpush.api.examples;

import java.util.HashMap;
import java.util.Map;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.SMS;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.report.MessagesResult;
import com.google.gson.JsonObject;
import com.thinkgem.jeesite.modules.cms.entity.Article;
import org.apache.commons.logging.Log;

public class PushExample {
//    protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

    // demo App defined in resources/jpush-api.conf 
	private static final String appKey ="ff0471efcc2d8f12be9fcad3";
	private static final String masterSecret = "644bea41a486d44f57076795";
	
	public static final String TITLE = "测试title";
    public static final String ALERT = "测试 - alert";
    public static final String MSG_CONTENT = "新闻测试 msgContent";
    public static final String REGISTRATION_ID = "0900e8d85ef";
    public static final String TAG = "tag_api";

	public static void main(String[] args) {
//        testSendPushWithCustomConfig();
//        testSendIosAlert();
		testSendPush(new Article());
	}
//

	public static void testSendPush(Article article) {
	    // HttpProxy proxy = new HttpProxy("localhost", 3128);
	    // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
		ClientConfig clientConfig = ClientConfig.getInstance();
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
        // For push, all you need do is to build PushPayload object.
//        PushPayload payload = buildPushObject_android_tag_alertWithTitle();
//        PushPayload payload = buildPushObject_all_all_alert(article);
//        PushPayload payload = buildPushObject_android_tag_alertWithTitle(article);
//        PushPayload payload = builPushObject_all_all_msg();
//        PushPayload payload = buildPushObject_ios_audienceMore_messageWithExtras();
        PushPayload payload = buildPushObject_android_and_ios(article);



        try {
            PushResult result = jpushClient.sendPush(payload);
        } catch (cn.jpush.api.common.resp.APIConnectionException e) {
            e.printStackTrace();
        } catch (cn.jpush.api.common.resp.APIRequestException e) {
            e.printStackTrace();
        }

    }
	
	public static PushPayload buildPushObject_all_all_alert(Article article) {
//        return PushPayload.newBuilder()
//                .setPlatform(Platform.all())
//                .setAudience(Audience.all())
//                .setMessage(Message.newBuilder()
//                .setTitle(TITLE)
//                .setMsgContent(MSG_CONTENT)
//                .addExtra("url","www.baidu.com")
//                .build())
//                .build();
//        PushPayload.newBuilder().setAudience(Audience.all()).setPlatform(Platform.all()).build();
	    return PushPayload.alertAll(ALERT);

	}

    public static Message builPushObject_all_all_msg(){

        return Message.newBuilder()
                .setMsgContent(MSG_CONTENT)
                .setTitle(TITLE).build();


    }
	
    public static PushPayload buildPushObject_all_alias_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias("alias1"))
                .setNotification(Notification.alert(ALERT))
                .build();
    }
    
    public static PushPayload buildPushObject_android_tag_alertWithTitle(Article article) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("newsurl","/f/view-"+ article.getCategory().getId() +"-" + article.getId() + ".html");
//        map.put("commentNum","0");
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
//                .setNotification(Notification.newBuilder().addPlatformNotification())
                .setNotification(Notification.android(article.getDescription(),article.getTitle(),map))
                .build();
    }
    
    public static PushPayload buildPushObject_android_and_ios(Article article) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("newsurl","/f/view-"+ article.getCategory().getId() +"-" + article.getId() + ".html");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                		.setAlert(article.getDescription())
                		.addPlatformNotification(AndroidNotification.newBuilder()
                				.setTitle(article.getTitle()).addExtras(map).build())
                		.addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(article.getTitle()).addExtra("newsurl", "/f/view-"+ article.getCategory().getId() +"-" + article.getId() + ".html").build())
                		.build())
                .build();
    }

    public static void buildPushObject_with_extra() {

        JsonObject jsonExtra = new JsonObject();
        jsonExtra.addProperty("extra1", 1);
        jsonExtra.addProperty("extra2", false);

        Map<String, String> extras = new HashMap<String, String>();
        extras.put("extra_1", "val1");
        extras.put("extra_2", "val2");

        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag("tag1"))
                .setNotification(Notification.newBuilder()
                        .setAlert("alert content")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle("Android Title")
                                .addExtras(extras)
                                .addExtra("booleanExtra", false)
                                .addExtra("numberExtra", 1)
                                .addExtra("jsonExtra", jsonExtra)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("extra_key", "extra_value").build())
                        .build())
                .build();

        System.out.println(payload.toJSON());
    }
    
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and("tag1", "tag_all"))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(ALERT)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                 .setMessage(Message.content(MSG_CONTENT))
                 .setOptions(Options.newBuilder()
                         .setApnsProduction(true)
                         .build())
                 .build();
    }
    
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
                        .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
                        .build())
                .setMessage(Message.newBuilder()
                        .setTitle(TITLE)
                        .setMsgContent(MSG_CONTENT)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }

//    public static void testSendPushWithCustomConfig() {
//        ClientConfig config = ClientConfig.getInstance();
//        // Setup the custom hostname
//        config.setPushHostName("https://api.jpush.cn");
//
//        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
//
//        // For push, all you need do is to build PushPayload object.
////        PushPayload payload = buildPushObject_all_all_alert();
//
//        try {
//            PushResult result = jpushClient.sendPush(payload);
//        } catch (cn.jpush.api.common.resp.APIConnectionException e) {
//            e.printStackTrace();
//        } catch (cn.jpush.api.common.resp.APIRequestException e) {
//            e.printStackTrace();
//        }
//
//    }

    public static void testSendIosAlert() {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        IosAlert alert = IosAlert.newBuilder()
                .setTitleAndBody("test alert", "test ios alert json")
                .setActionLocKey("PLAY")
                .build();
        try {
            PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap<String, String>(), "alias1");
        } catch (cn.jpush.api.common.resp.APIConnectionException e) {
            e.printStackTrace();
        } catch (cn.jpush.api.common.resp.APIRequestException e) {
            e.printStackTrace();
        }
    }

    public static void testSendWithSMS() {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
        try {
            SMS sms = SMS.content("Test SMS", 10);
            PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
        } catch (cn.jpush.api.common.resp.APIConnectionException e) {
            e.printStackTrace();
        } catch (cn.jpush.api.common.resp.APIRequestException e) {
            e.printStackTrace();
        }
    }

}

